//
//  HLEditAvatarCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLEditAvatarCell.h"
#import "HLPickerManager.h"

@interface HLEditAvatarCell ()

@property (nonatomic, strong) UIImageView *uploadImageView;
@property (nonatomic, strong) HLPickerManager *pickerManager;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

@end

@implementation HLEditAvatarCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = HLBackgroundColor;
        self.pickerManager = [[HLPickerManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto avatar:YES];
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    
    self.uploadImageView = [[UIImageView alloc] init];
    [_uploadImageView setBackgroundColor:[UIColor qmui_colorWithHexString:@"#131415"]];
    _uploadImageView.contentMode = UIViewContentModeScaleAspectFill;
    _uploadImageView.layer.masksToBounds = YES;
    _uploadImageView.layer.cornerRadius = 60;
    _uploadImageView.layer.borderColor = [HLMainColor colorWithAlphaComponent:0.4].CGColor;
    _uploadImageView.layer.borderWidth = 4;
    _uploadImageView.image = [UIImage imageNamed:@"avatar_male_big_placeholder"];
    _uploadImageView.userInteractionEnabled = YES;
    [self.contentView addSubview:_uploadImageView];
    [_uploadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.bottom.mas_equalTo(-24);
        make.size.mas_equalTo(120);
        make.centerX.mas_equalTo(0);
    }];
    
    
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(uploadAction)];
    [_uploadImageView addGestureRecognizer:self.tapGesture];
    
}

- (void)setModel:(HLUserInfoModel *)model
{
    _model = model;
    
    if (model.avatarImage){
        _uploadImageView.image = model.avatarImage;
    } else {
        [_uploadImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar_male_big_placeholder"]];
    }
}

- (void)previewAvatar
{
    HXPhotoManager *photoManager = [HXPhotoManager managerWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    NSMutableArray *models = [NSMutableArray array];
    if (_model.avatarImage){
        HXCustomAssetModel *m = [HXCustomAssetModel assetWithLocalImage:_model.avatarImage selected:YES];
        [models addObject:m];
    } else {
        HXCustomAssetModel *m = [HXCustomAssetModel assetWithNetworkImageURL:[NSURL URLWithString:_model.avatar] selected:YES];
        [models addObject:m];
    }
    
    [photoManager addCustomAssetModel:models];
    [HLPickerManager previewWithManager:photoManager index:0 controller:self.qmui_viewController];
    
}

- (void)uploadAction
{
    MJWeakSelf;
    [self.pickerManager showWithController:self.qmui_viewController block:^(NSArray<HXPhotoModel *> *photos) {
        HXPhotoModel *photo = [photos firstObject];
        [photo getImageWithSuccess:^(UIImage * _Nullable image, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
            weakSelf.model.avatarImage = image;
            weakSelf.uploadImageView.image = image;
        } failed:nil];
    }];
}


@end
